About instanceof operator
The instanceof operator is a binary operator that is used to check if an object is an instance of a particular class or interface. The syntax of the instanceof operator is as follows:
object instanceof class
The object operand is the object that is being checked. The class operand is the class or interface that is being checked against.
The instanceof operator returns a Boolean value. The value is true if the object is an instance of the class or interface, and false if it is not.
Here is an example of how the instanceof operator can be used to check if an object is an instance of the String class:
Java
String str = "Hello, world!";
// Check if str is an instance of String
boolean isString = str instanceof String;
In this example, the str variable is an instance of the String class. Therefore, the isString variable will be assigned the value true.
The instanceof operator can be used to perform a variety of operations, such as:
Checking if an object is an instance of a particular class
Checking if an object is an instance of an interface
Determining the type of an object
Casting an object to a different type
The instanceof operator can be a useful tool for working with objects in Java. However, it is important to use it carefully and to make sure that the code is clear and understandable.
Example for InstanceOf() operator
Example 1: Polymorphism and instanceof
public class Main{
public static void main(String[] args) {
Shape[] shapes = { new Circle(), new Triangle(), new Shape() };
for (Shape shape : shapes) {
if (shape instanceof Circle) {
System.out.println("This is a circle.");
} else if (shape instanceof Triangle) {
System.out.println("This is a triangle.");
} else if (shape instanceof Shape) {
System.out.println("This is a generic shape.");
}
}
}
}
class Shape {
void draw() {
System.out.println("Drawing a shape.");
}
}
class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing a circle.");
}
}
class Triangle extends Shape {
@Override
void draw() {
System.out.println("Drawing a triangle.");
}
}
Output
This is a circle.
This is a triangle.
This is a generic shape.
In this example, we have a class hierarchy consisting of a Shape class and its subclasses Circle and Triangle. The instanceof operator is used to determine the actual runtime type of each object in the shapes array. This demonstrates how instanceof can be used to check for inheritance relationships and polymorphic behavior.
Example 2
Example 2: Custom Class Hierarchy and Interface
public class Main{
public static void main(String[] args) {
Eatable[] items = { new Fruit(), new Vegetable() };
for (Eatable item : items) {
if (item instanceof Fruit) {
System.out.println("This is a fruit.");
} else if (item instanceof Vegetable) {
System.out.println("This is a vegetable.");
}
item.eat(); // Call the common eat method
}
}
}
interface Eatable {
void eat();
}
class Fruit implements Eatable {
@Override
public void eat() {
System.out.println("Eating a fruit.");
}
}
class Vegetable implements Eatable {
@Override
public void eat() {
System.out.println("Eating a vegetable.");
}
}
Output
This is a fruit.
Eating a fruit.
This is a vegetable.
Eating a vegetable.
In this example, we have an interface Eatable implemented by classes Fruit and Vegetable. The instanceof operator is used to determine the specific type of object in the items array and then calls the eat method accordingly. This demonstrates how instanceof can be used to differentiate between different implementations of an interface.
Here are some additional details about the instanceof operator in Java:
The instanceof operator is evaluated from left to right.
The object operand must be of a reference type.
The class operand must be a class or interface.
The instanceof operator can be used to check if an object is an instance of a class or interface that is inherited from the specified class or interface.